-- #########################################
-- ##### Inject my options configurations into the default WoW UI
function ClassButtons:LoadOptions()
	local panel = ClassButtons_Options_Panel

	local HBar = ClassButtons:CreateCheckButton("Horizontal bar", panel, ClassButtonsDB, "HorizontalBar") 
	HBar:SetPoint('TOPLEFT', 10, -30)
	HBar:SetChecked(ClassButtonsDB.HorizontalBar)
	
	local Header = HBar:CreateFontString(nil, "BACKGROUND")
	Header:SetFontObject(GameFontNormalHuge)
	Header:SetWidth(400)
	Header:SetHeight(12)
	Header:SetPoint('TOPLEFT', 10, 20)
	Header:SetJustifyH("LEFT")
	Header:SetJustifyV("TOP")
	Header:SetText(panel.name)
	
	local VBar = ClassButtons:CreateCheckButton("Vertical bar", panel, ClassButtonsDB, "VerticalBar") 
	VBar:SetPoint('TOP', HBar, 'BOTTOM', 0, 0)
	VBar:SetChecked(ClassButtonsDB.VerticalBar)
	
	local Tooltips = ClassButtons:CreateCheckButton("Show tooltips", panel, ClassButtonsDB, "Tooltips") 
	Tooltips:SetPoint('TOP', VBar, 'BOTTOM', 0, 0)
	Tooltips:SetChecked(ClassButtonsDB.Tooltips)
	
	local Anchor = ClassButtons:CreateCheckButton("Show anchors", panel, ClassButtonsDB, "Anchor") 
	Anchor:SetPoint('TOP', Tooltips, 'BOTTOM', 0, 0)
	Anchor:SetChecked(ClassButtonsDB.Anchor)
	
	local StealthMode = ClassButtons:CreateCheckButton("Fly in key-bound stealth mode", panel, ClassButtonsDB, "StealthMode") 
	StealthMode:SetPoint('TOP', Anchor, 'BOTTOM', 0, 0)
	StealthMode:SetChecked(ClassButtonsDB.StealthMode)
	
	local DarkArea0 = ClassButtons:CreateDarkArea("Button Size Options", panel, 397, 100)
	DarkArea0:SetPoint('TOPLEFT', StealthMode, 'BOTTOMLEFT', 0, 0)
	
	local VerticalScale = ClassButtons:CreateSlider("Vertical Bar Scale", panel, .25, 3, .15, ClassButtonsDB, "VerticalScale")
	VerticalScale:SetPoint('TOPLEFT', DarkArea0, 'TOPLEFT', 20, -40)
	VerticalScale:SetScript('OnValueChanged', function(self, value)
		self.valText:SetText(format('%.1f', value))
		if not self.onShow then
			ClassButtonsDB.VerticalScale = value
			
			for i = 1, 48 do
				local frame = _G["ClassButton"..i]
				if frame then frame:SetScale(ClassButtonsDB.VerticalScale) end
			end
		end
	end)
	VerticalScale:SetValue(ClassButtonsDB.VerticalScale)
	
	local ScaleReset0 = ClassButtons:CreateButton("reset vert", panel, 115, 20)
	ScaleReset0:SetPoint('LEFT', VerticalScale, 'RIGHT', 100, 0)
	ScaleReset0:SetScript('OnClick', function() ClassButtonsDB.VerticalScale = 1; VerticalScale:SetValue(1) end)
	
	local HorizontalScale = ClassButtons:CreateSlider("Horizontal Bar Scale", panel, .25, 3, .15, ClassButtonsDB, "HorizontalScale")
	HorizontalScale:SetPoint('TOP', VerticalScale, 'BOTTOM', 0, -15)
	HorizontalScale:SetScript('OnValueChanged', function(self, value)
		self.valText:SetText(format('%.1f', value))
		if not self.onShow then
			ClassButtonsDB.HorizontalScale = value
			
			for i = 49, 60 do
				local frame = _G["ClassButton"..i]
				if frame then frame:SetScale(ClassButtonsDB.HorizontalScale) end
			end
		end
	end)
	HorizontalScale:SetValue(ClassButtonsDB.HorizontalScale)
	
	local ScaleReset1 = ClassButtons:CreateButton("reset horiz", panel, 115, 20)
	ScaleReset1:SetPoint('LEFT', HorizontalScale, 'RIGHT', 100, 0)
	ScaleReset1:SetScript('OnClick', function() ClassButtonsDB.HorizontalScale = 1; HorizontalScale:SetValue(1) end)
	
	local AutoPosition = ClassButtons:CreateButton("Easy Position 1", panel, 125, 30)
	AutoPosition:SetPoint('TOPLEFT', 10, -390)
	AutoPosition:SetScript('OnClick', function()
		ClassButtonsAnchor1:ClearAllPoints(); ClassButtonsAnchor1:SetPoint("RIGHT", "MultiBarLeftButton1", "LEFT", -6, 41)
		ClassButtonsAnchor2:ClearAllPoints(); ClassButtonsAnchor2:SetPoint("BOTTOM", "MultiBarBottomRightButton12", "TOP", 41, 6)
		ClassButtonsAnchor1:SetUserPlaced(true); ClassButtonsAnchor2:SetUserPlaced(true)
	end)
	
	local AutoPosition2 = ClassButtons:CreateButton("Easy Position 2", panel, 125, 30)
	AutoPosition2:SetPoint('LEFT', AutoPosition, 'RIGHT', 0, 0)
	AutoPosition2:SetScript('OnClick', function()
		ClassButtonsAnchor2:ClearAllPoints()
		ClassButtonsAnchor2:SetPoint("BOTTOM", "MultiBarBottomLeftButton12", "TOP", 41, 6)
		ClassButtonsAnchor2:SetUserPlaced(true)
	end)
	
	ClassButtons:HideCheck()
end 

function ClassButtons:CreateButton(name, parent, width, height)
	local button = CreateFrame('Button', parent:GetName() .. name, parent, 'UIPanelButtonTemplate')
	button:SetWidth(width)
	button:SetHeight(height)
	
	local frame = _G[button:GetName() .. 'Text']
	frame:SetText(name)
	frame:SetTextColor(1, 1, 1, 1)

	return button 
end

function ClassButtons:CreateCheckButton(name, parent, storage, field) 
	local button = CreateFrame('CheckButton', parent:GetName() .. name, parent, 'OptionsCheckButtonTemplate') 
	
	local frame = _G[button:GetName() .. 'Text']
	frame:SetText(name)
	frame:SetTextColor(1, 1, 1, 1)
	frame:SetFontObject(GameFontNormal)
	
	button:SetScript("OnShow", 
		function (self) 
			self:SetChecked(storage[field])
			self.origValue = storage[field]
		end 
	)
	button:SetScript("OnClick", 
		function (self, button, down) 
			storage[field] = not storage[field] 
		end 
	) 
	function button:Restore() 
		if self.origValue ~= nil then storage[field] = self.origValue end
	end 
 
	return button 
end

function ClassButtons:CreateSlider(text, parent, low, high, step, storage, field)
	local name = parent:GetName() .. text
	local slider = CreateFrame('Slider', name, parent, 'OptionsSliderTemplate')
	slider:SetScript('OnMouseWheel', Slider_OnMouseWheel)
	slider:SetMinMaxValues(low, high)
	slider:SetValueStep(step)
	slider:EnableMouseWheel(true)

	_G[name .. 'Text']:SetText(text)
	_G[name .. 'Low']:SetText('')
	_G[name .. 'High']:SetText('')

	local text = slider:CreateFontString(nil, 'BACKGROUND')
	text:SetFontObject('GameFontHighlightSmall')
	text:SetPoint('LEFT', slider, 'RIGHT', 7, 0)
	slider.valText = text

	slider:SetScript('OnShow', function(self)
		self.origValue = storage[field]
		self.onShow = true
		self:SetValue(storage[field])
		self.onShow = nil
	end)
	
	function slider:Restore() 
		if self.origValue ~= nil then storage[field] = self.origValue end
		
		for i = 1, 60 do
			local frame = _G["ClassButton"..i]
			if frame then
				frame:SetScale(self.origValue)
			end
		end
		
	end 
	
	return slider
end

function ClassButtons:CreateDarkArea(text, parent, width, height)
	local name = parent:GetName() .. text
	local area = CreateFrame("Frame", name, parent)
	area:SetWidth(width)
	area:SetHeight(height)
	
	area:SetBackdrop({bgFile = "Interface/DialogFrame/UI-DialogBox-Background", 
								--edgeFile = "Interface/DialogFrame/UI-DialogBox-Border",
								insets = { left = 5, right = 6, top = 6, bottom = 5 }}
	)
	
	local header = area:CreateFontString(nil, "BACKGROUND")
	header:SetFontObject(GameFontNormal)
	header:SetWidth(width)
	header:SetHeight(12)
	header:SetPoint('TOPLEFT', 10, -10)
	header:SetJustifyH("LEFT")
	header:SetJustifyV("TOP")
	header:SetText(text)
	
	return area
end